<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>评论列表</title>
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
  <link rel="stylesheet" href="./css/reset.css" />
  <link rel="stylesheet" href="./css/iconfont.css" />
  <link rel="stylesheet" href="./css/main.css" />
  <link rel="stylesheet" href="./libs/layui/css/layui.css">

  <script src="./libs/jquery/jquery.min.js"></script>
  <script src="./libs/art-template/template-web.js"></script>


  <script src="./js/jqconfig.js"></script>
  <script src="./libs/layui/layui.js"></script>
</head>

<body>
  <div class="container-fluid">
    <div class="common_title">评论列表</div>
    <div class="container-fluid common_con">
      <table class="table table-striped table-bordered table-hover mp20">
        <thead>
          <tr>
            <th style="width: 7%">作者</th>
            <th style="width: 25%">评论</th>
            <th style="width: 25%">评论在</th>
            <th style="width: 15%">提交于</th>
            <th style="width: 6%">状态</th>
            <th class="text-center" style="width: 12%">操作</th>
          </tr>
        </thead>
        <tbody>
          <tr class="danger"></tr>
          <tr>
            <td>武秀英</td>
            <td>
              外影广条同取水权科速工与。矿身面却属次养导务作者用品油调。高石期品极放存斗一号口消别共去。
            </td>
            <td>《世界，你好》</td>
            <td>1970-03-15 11:31:50</td>
            <td>已拒绝</td>
            <td class="text-center">
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr>
        </tbody>
      </table>
      <div id="pager" class="row text-center"></div>
    </div>
  </div>

  <script id="art_comment" type="text/html">
      {{each data}} {{if $value.state ==='已拒绝'}}
      <tr class="danger">
        {{else }}
      <tr>
        {{/if}}
        <td>{{$value.author}}</td>
        <td>{{$value.content}}</td>
        <td>{{$value.title}}</td>
        <td>{{$value.date}}</td>
        <td>{{$value.state}}</td>
        <td class="text-center">
          {{if $value.state ==='已拒绝'||$value.state ==='待审核'}}
          <a
            href="javascript:void(0);;"
            class="btn btn-success btn-xs btnAccept"
            onclick="btnAccept('{{$value.id}}')"
            >批准</a
          >
          {{/if}} {{if $value.state ==='已通过'||$value.state ==='待审核'}}
          <a href="javascript:void(0);" class="btn btn-warning btn-xs btnReject" onclick="btnReject('{{$value.id}}')"
            >拒绝</a
          >
          {{/if}}
          <a href="javascript:void(0);;" class="btn btn-danger btn-xs btnDelete" onclick="btnDelete('{{$value.id}}')"
            >删除</a
          >
        </td>
      </tr>
      {{/each}}
    </script>
  <script>
    let params = { page: 1, perpage: 6 };
    let laypage, layer;
    layui.use(['laypage', 'layer'], function () {
      laypage = layui.laypage;
      layer = layui.layer;
      get_comment_list();

    });

    //1.获取所有评论

    function get_comment_list() {
      $.ajax({
        type: "get",
        url: "/admin/comment/search",
        data: params,
        success: (res) => {
          // console.log(res);
          $("tbody").html(template("art_comment", res.data));

          //2.页面脚
          laypage.render({
            elem: 'pager' //注意，这里的 test1 是 ID，不用加 # 号
            , count: res.data.totalCount//数据总数，从服务端得到
            , limit: params.perpage
            , curr: params.page //显示当前的页码的页面
            , jump: function (obj, first) {
              //obj包含了当前分页的所有参数，比如：
              // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
              // console.log(obj.limit); //得到每页显示的条数

              //首次不执行
              if (!first) {
                let curr = obj.curr
                params.page = curr
                get_comment_list()
              }
            }
          });
        },
      });
    }
    //审核通过
    function btnAccept(id) {
      $.ajax({
        type: 'post',
        url: '/admin/comment/pass',
        data: { id },
        success: (res) => {
          get_comment_list();
        },
      })
    }
    // 评论审核不通过
    function btnReject(id) {
      $.ajax({
        type: 'post',
        url: '/admin/comment/reject',
        data: { id },
        success: (res) => {
          get_comment_list();
        },
      })
    }
    //评论删除功能
    function btnDelete(id) {
      layer.confirm('是否删除?', { icon: 3, title: '删除确认' }, function (index) {
        $.ajax({
          type: 'post',
          url: '/admin/comment/delete',
          data: { id },
          success: (res) => {
            get_comment_list();
          },
        })
        layer.close(index);
      });

    }
  </script>
</body>

</html>